<?php

$task_obj = new task_obj();
$task_phorum_obj = new task_phorum_obj();
$client_obj = new client_obj();
$record_obj = new record_obj();
$tariff_obj = new tariff_obj();
$lock_obj = new lock_obj();

if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) {
    if ($_REQUEST['action'] == 'add_record') {

        $date = explode('/', $_REQUEST['record']['date']);
        $filter = array(
            'month' => $date[1],
            'year' => $date[2],
            'type' => 'record'
        );

        $is_locked = $lock_obj->getLocks($filter);
        if (error_obj::isError($is_locked)) {
            error_obj::setError($is_locked, UNEXCEPTED);
        } elseif (!empty($is_locked)) {
            error_obj::setError($is_locked, PERIOD_IS_LOCKED);
        } else {
            $in_data = $_REQUEST['record'];
            $in_data['unit_price'] = $in_data['price'];
            unset($in_data['price']);
            $in_data['date'] = base_obj::convertDateTo($in_data['date']);
            $newId = $record_obj->addRecord($in_data);
            if (error_obj::isError($newId)) {
                error_obj::setError($newId, UNEXCEPTED);
            } else {
                if (empty($_SESSION['request']['newRecords']))
                    $_SESSION['request']['newRecords'] = array();
                $_SESSION['request']['newRecords'][$newId] = $newId;
                $change_obj = new change_obj();
                $in_data = array(
                    'rel_id' => $newRecordId,
                    'type' => CHANGES_TYPE_TSDR,
                    'status' => CHANGES_STATUS_NEW,
                    'date' => time()
                );
                $change_obj->add_change($in_data);
                base_obj::redirect(ROOT . '?input=add_record');
            }
        }
    }
}

$prefill = array();

if (isset($_REQUEST['copy_id']) && !empty($_REQUEST['copy_id']) && is_numeric($_REQUEST['copy_id']) && ($_REQUEST['copy_id'] > 0)) {
    $recordId = (int) $_REQUEST['copy_id'];
    if (!empty($recordId)) {
        $result = $record_obj->getRecordById($recordId);
        if (!error_obj::isError($result) && !empty($result)) {
            $prefill = $result;
            $prefill['type_of_prefill'] = 'record';
        }
    }
} elseif (isset($_SESSION['request']['newRecordFromTask']) && !empty($_SESSION['request']['newRecordFromTask'])) {
    $taskId = (int) $_SESSION['request']['newRecordFromTask'];
    if (!empty($taskId)) {
        $result = $task_obj->getTaskById($taskId);
        if (!error_obj::isError($result) && !empty($result)) {
            $prefill = $result;
            $filter = array();
            $filter['task_id'] = $prefill['taskId'];
            $prefill['phorum'] = $task_phorum_obj->getPostsInPhorum($filter, "task_phorum_id ASC", '0,1');
            $prefill['type_of_prefill'] = 'task';
            unset($_SESSION['request']['newRecordFromTask']);
        }
    }
}

$newRecords = array();
if (!empty($_SESSION['request']['newRecords'])) {
    foreach ($_SESSION['request']['newRecords'] as $key => $recordId) {
        $newRecord = $record_obj->getRecordById($recordId);
        if (error_obj::isError($newRecord)) {
            error_obj::setError($newRecord, UNEXCEPTED);
        } elseif (!empty($newRecord)) {
            $newRecord['date_formated'] = base_obj::convertDateTo($newRecord['date'], 'dd/mm/yyyy', 'date');
            $newRecord['client_info'] = $client_obj->getClient($newRecord['client'], array("name"));
            $newRecord['type_info'] = $record_obj->getTypeOfRecordById($newRecord['type'], array("type_of_record"));
            if ($newRecord['tariff'] != -1)
                $newRecord['tariff_info'] = $tariff_obj->getTariffById($newRecord['tariff'], array("title"));
            $newRecords[] = $newRecord;
        }
    }
}

$GLOBALS['template_data']['newRecords'] = $newRecords;

if (isset($prefill['type_of_prefill'])) {

    $month = date("m");
    $year = date("Y");
    $client_obj = new client_obj();
    $db_obj = new db_obj();
    $res = $client_obj->getClient($prefill['client'], array('conditions'));

    $text = '';

    if (!error_obj::isError($res) && !empty($res)) {
        $text .= '<div><h3>Conditions:</h3>';
        if (empty($res['conditions']))
            $text .= '-';
        else
            $text .= $res['conditions'];
        $text .= '</div>';
    }

    $sql = "SELECT SUM(amount) AS s FROM " . $GLOBALS['tb_records'] . " WHERE client=" . $prefill['client'] . " AND DATE_FORMAT(date, '%m')='" . $month . "' AND DATE_FORMAT(date, '%Y')='" . $year . "'";
    $res = $db_obj->getOne($sql);
    if (!error_obj::isError($res) && !empty($res)) {
        $text .= '<div><h3>Used amount in ' . $month . '/' . $year . ':</h3>';
        $text .= $res;
        $text .= '</div>';
    } else {
        $text .= '<div><h3>Used amount in ' . $month . '/' . $year . ':</h3>';
        $text .= '0';
        $text .= '</div>';
    }

    if ($prefill['type_of_prefill'] == 'task') {
        $prefill['date'] = date("d/m/Y");
        $prefill['amount'] = $prefill['duration'];
        $prefill['description'] = $prefill['phorum'][0]['text'];
    } else {
        $prefill['date'] = base_obj::convertDateTo($prefill['date'], 'dd/mm/yyyy', 'date');
    }
    $prefill['report'] = $text;
}
$GLOBALS['template_data']['prefill'] = $prefill;

$filter = array();
$filter['active'] = 0;
$GLOBALS['template_data']['clients'] = $client_obj->getAllClients('klient_id, name', $filter, 'name ASC');
$GLOBALS['template_data']['types'] = $record_obj->getRecordTypes();
$filter = array();
$filter['type_of_record'] = 1;
$GLOBALS['template_data']['tariffs'] = $tariff_obj->getTariffs("Id, title", $filter, 'title ASC');
?>
